/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.stepbystep;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * @author zinin
 */
public class MIDPCanvas extends GameCanvas implements CommandListener, Runnable{
    // device screen geometry and some constants that are shared between classes
    public int CORNER_X = 0;
    public int CORNER_Y = 0;
    public int DISP_WIDTH;
    public int DISP_HEIGHT;
    public static int GRID_WIDTH = 16;
    public static final int BCGRND_COLOR = 0xffffffff;
    
    //create layout manager and pass screen geometry there
    private GameLayerManager layerManager = null;
    private Graphics graphics = getGraphics();
    private int sleepTime = 10;
    private volatile Thread thread;

    /**
     * constructor
     */
    public MIDPCanvas() {
        super(false);
        try {
            // Set up this canvas to listen to command events
            setCommandListener(this);
            // Add the Exit command
            addCommand(new GameCommand("Exit", Command.EXIT, 1));

            setFullScreenMode(false);
            DISP_WIDTH = getWidth();
            DISP_HEIGHT = getHeight();
            
            layerManager = new GameLayerManager(this);
            
            // for backward compatibility with keyboard devices
            try{
                Class.forName("com.nokia.mid.ui.gestures.GestureRegistrationManager");
                // This is a touch device
                new MyGesture(this);
            }catch (Throwable e){
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void checkKeys() {
        int keyState = getKeyStates();
        if ((keyState & LEFT_PRESSED) != 0) {
            layerManager.setPlayerMovingLeft(true);
        }
        if ((keyState & RIGHT_PRESSED) != 0) {
            layerManager.setPlayerMovingLeft(false);
        }
        if ((keyState & UP_PRESSED) != 0) {
            layerManager.setPlayerMovingUp(true);
        }
        if ((keyState & DOWN_PRESSED) != 0) {
            layerManager.setPlayerMovingUp(false);
        }
    }

    GameLayerManager getLayerManager() {
        return layerManager;
    }

    public void commandAction(Command c, Displayable d) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    class GameCommand extends Command {

        public GameCommand(String label, int commandType, int priority) {
            super(label, commandType, priority);
        }
    }
 
    // When the game canvas is hidden, stop the thread.
    protected void hideNotify() {
        thread = null;
    }

    // When the canvas is shown, start a thread to
    // run the game loop.
    protected void showNotify() {
        thread = new Thread(this);
        thread.start();
    }

    // The game loop.
    public void run() {
        try {
            while (thread != null) {
                checkKeys();
                layerManager.paint(graphics);
                flushGraphics();
                Thread.currentThread().sleep(sleepTime);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
